from bs4 import BeautifulSoup
from urllib.parse import urljoin
import requests
import csv


URL = 'http://xa.ganji.com/fang1/'  # 定义爬取得目标    o%7Bpage%7Dp%7Bprice%7D/
ADDR = 'http://xa.ganji.com'

if __name__ == '__main__':
    start_page = 1  # 开始爬取得页面
    end_page = 10  # 结束爬取得页面
    price = 7  # 价格
    with open('ganji.csv', 'w') as f:  # 创建一个csv文件
        csv_writer = csv.writer(f, delimiter=',')
        print('start=================================>')
        while start_page <= end_page:
            start_page += 1
            print('get:{0}'.format(URL.format(page=start_page, price=price)))
            res = requests.get(URL.format(page=start_page, price=price))
            html = BeautifulSoup(res.text, 'html.parser')  # 第一个参数要抓取的html文本，第二个是使用那种解析器
            house_list = html.select('.f-list > .f-list-item > .f-list-item-wrap')  # 获取房源信息
            if not house_list:
                break
            for house in house_list:
                house_title = house.select('.title > a')[0].text
                house_addr = house.select('.address > .area > a')[-1].text
                house_price = house.select('.info > .price > .num')[0].text
                house_url = urljoin(ADDR, house.select('.title > a')[0]['href'])
                csv_writer.writerow([house_title, house_addr, house_price, house_url])
        print('end===================================.')

                

